package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import metier.Spectacle;

public class DaoSpectacle 
{

	public int AjoutSpectacle (Spectacle unSpectacle) throws ClassNotFoundException, SQLException
	{
		String chaine = "jdbc:mysql://localhost/";
		String req;
		Connection connec;
		connec = MySqlConnection.getConnection(chaine + "mlr1", "root", "");
		req = "insert into spectacle values (" + unSpectacle.getNumSpect() + ", '" + unSpectacle.getNomSpect() + "' ," + unSpectacle.getDate()
				+ "" + unSpectacle.getTarif() + ")";
		System.out.println(req);
		return 1;
	}
	
	public static Vector <Spectacle> getLesSpectacles () throws ClassNotFoundException, SQLException
	{
		String chaine = "jdbc:mysql://localhost/";
		Statement stLienBd = null;
		ResultSet res = null;
		String req;
		Connection connec;
		Vector <Spectacle> lesSpectacles;
		Spectacle leSpectacle;
		connec = MySqlConnection.getConnection(chaine + "mlr1", "root", "");
		stLienBd = connec.createStatement();
		lesSpectacles = new Vector <Spectacle>();
		try
		{
			
			req = "select * from spectacle";
			res = stLienBd.executeQuery(req);
			while (res.next())
			{
				leSpectacle = new Spectacle (res.getInt(1), res.getString(2), res.getString(3), res.getDouble(4));
				lesSpectacles.add(leSpectacle);
			}
			res.close();
			connec.close();
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
		return lesSpectacles;
	}
	

}
